package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.entity.vo.*;
import edu.suda.xsap.service.MemberBindRecordService;
import edu.suda.xsap.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import edu.suda.xsap.service.MemberCardService;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/card-management")
@Slf4j
public class MemberCardController {
    @Autowired private MemberCardService memberCardService;
    @Autowired
    private MemberBindRecordService memberBindRecordService;
    @Autowired
    private MemberService memberService;
    /** axios.get('/api/user-management/get-cards'**/
    @GetMapping ("/get-cards")
    public Result getCards() {
        try {
            System.out.println(memberCardService.findAll());
            return Result.success(memberCardService.findAll());
        }
        catch (Exception e) {
            log.error("获取会员卡信息失败", e);
            return Result.failure("获取会员卡信息失败");
        }

    }

    /**axios.post('/api/card-management/card-bind'发送绑定信息到后端*/
    @PostMapping("/card-bind")
    public Result cardBind(@RequestBody BIndVo vo) {
        try {
            System.out.println(vo);
            memberCardService.bind(vo);
            return Result.success();
        }
        catch (Exception e) {
            log.error("绑定会员卡失败", e);
            return Result.failure("绑定会员卡失败");
        }
    }
    @PostMapping("/change-status")
    public Result changeStatus(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            Integer s=memberService.changeStatus(id);
            return Result.success(s);
        }catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /***ard-management/get-total-cards*/
    @GetMapping("/get-total-cards")
    public Result getTotalCards() {
        try {
            Integer count = memberCardService.findCount();
            return Result.success(count);
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    /***card-management/handle-card-page-change**/
    @PostMapping("/handle-card-page-change")
    public Result handleCardPageChange(@RequestBody Page2 page2) {
        try {
            System.out.println(page2);
            List<CardVo> memberCardVos = memberCardService.findByLimit(page2.getPageSize(), (page2.getPage() - 1) * page2.getPageSize());
            System.out.println(memberCardVos);
            return Result.success(memberCardVos);
        } catch (Exception e){
            return Result.failure(e.getMessage(), e);
        }
    }


    @PostMapping("/send-card-mes")
    public Result sendCardMes(@RequestBody NewCardVo vo) {
        try {
            System.out.println(vo);
            vo.setCreateTime(LocalDateTime.now());
            memberCardService.save1(vo);
            return Result.success();
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }



    /***'/course-management/get-card-course***/

}
